package com.shuqi.model.sharedprefs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.shuqi.application.ShuqiApplication;
import com.shuqi.common.utils.Log4an;
import com.shuqi.model.bean.AccountLoginHistoryInfo;

public class SpLoginHistory {
    /** SharedPreferences 文件名：登陆历史 */
    private final String SP_NAME_LOGINHISTORY = "loginHistory";
    
    private static SpLoginHistory history;
    private SpLoginHistory(){}
    
    public static SpLoginHistory getInstance(){
        if(history == null){
            history = new SpLoginHistory();
        }
        return history;
    }
    
    /***
     * 保存登陆历史
     * @param accountName
     * @return
     */
    public boolean clearLoginHistory(){
        SharedPreferences sp = ShuqiApplication.getContext().getSharedPreferences(SP_NAME_LOGINHISTORY, Context.MODE_PRIVATE);
        Editor edite = sp.edit();
        Log4an.e("spLoginHistory", "clearLoginHistory = "+edite.clear().commit());
        return false;
    }
    
    /***
     * 删除登陆历史
     * @param accountName
     * @return
     */
    public boolean deleteLoginHistory(String accountName){
        if(!TextUtils.isEmpty(accountName)){
            SharedPreferences sp = ShuqiApplication.getContext().getSharedPreferences(SP_NAME_LOGINHISTORY, Context.MODE_PRIVATE);
            return sp.edit().remove(accountName).commit();
        }
        return false;
    }
    
    /***
     * 保存登陆历史
     * @param accountName
     * @return
     */
    public boolean saveLoginAcoount(String accountName){
        if(!TextUtils.isEmpty(accountName)){
            SharedPreferences sp = ShuqiApplication.getContext().getSharedPreferences(SP_NAME_LOGINHISTORY, Context.MODE_PRIVATE);
            return sp.edit().putLong(accountName,System.currentTimeMillis()).commit();
        }
        return false;
    }
    
    /***
     * 获取登陆历史帐号
     * @return
     */
    public List<AccountLoginHistoryInfo> getAllLoginHistory(){
        long start = System.currentTimeMillis();
        SharedPreferences sp = ShuqiApplication.getContext().getSharedPreferences(SP_NAME_LOGINHISTORY, Context.MODE_PRIVATE);
        Map<String, Long> map = (Map<String, Long>) sp.getAll();
        List<AccountLoginHistoryInfo> list = sortData(map);
        Log4an.i("spLoginHistory", "size="+(list==null?"0":list.size())+", time="+(System.currentTimeMillis()-start));
        return list;
    } 
    
    /***
     * 对数据重组排序
     * @param map
     * @return
     */
    private List<AccountLoginHistoryInfo> sortData(Map<String, Long> map){
        List<AccountLoginHistoryInfo> list = null;
        if(map!=null && map.size()>0){
             list = new ArrayList<AccountLoginHistoryInfo>();
            Set<Entry<String, Long>> keySet = map.entrySet();
            for (Entry<String, Long> entry : keySet) {
                AccountLoginHistoryInfo info = new AccountLoginHistoryInfo();
                info.setLoginAccount(entry.getKey());
                info.setLoginTime(entry.getValue());
                list.add(info);
            }
            if(list.size()>0){
                Collections.sort(list, new ComparatorLoginHistory());
            }
        }
        return list;
    }
    
    /***
     * 登陆时间对象比较器
     * @author Administrator
     */
    private class ComparatorLoginHistory implements Comparator<AccountLoginHistoryInfo>{
        @Override
        public int compare(AccountLoginHistoryInfo object1, AccountLoginHistoryInfo object2) {
            AccountLoginHistoryInfo info1 = (AccountLoginHistoryInfo) object1;
            AccountLoginHistoryInfo info2 = (AccountLoginHistoryInfo) object2;
            return (int)(info2.getLoginTime()-info1.getLoginTime());
        }
    }
}
